package com.wanmait.heating.utils;

import org.springframework.util.StringUtils;

import java.util.Random;

public class EncryptUtils {
    //加密
    public static String encrypt(String str)
    {
        return getRandom(2)+str+getRandom(2);
    }

    //length为前后加的随机字符串的长度
    public static String encrypt(String str,int length)
    {
        return getRandom(length)+str+getRandom(length);
    }


    //解密
    public static String decrypt(String str)
    {
        if(str==null) return  null;
        String newStr = null;
        try {
            newStr = str.substring(2,str.length()-2);
        } catch (Exception e) {

        }
        return newStr;
    }

    public static String decrypt(String str,int length)
    {
        if(str==null) return  null;
        String newStr = null;
        try {
            newStr = str.substring(length,str.length()-length);
        } catch (Exception e) {

        }
        return newStr;
    }
    public static String getRandom(int length)
    {
        StringBuilder randomStr = new StringBuilder();
        String str = "123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            randomStr.append(str.charAt(random.nextInt(str.length())));
        }
        return randomStr.toString();
    }

    public static void main(String[] args) {
        //System.out.println(getRandom(3));
        String username = "aaa";
        String username1 = encrypt(null,3);
        System.out.println(username1);
        //System.out.println(decrypt(username1,3));
    }
}
